//
//  OAuthViewModel.swift
//  Weibo项目
//
//  Created by vn on 16/7/22.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit

class OAuthViewModel: NSObject {

    static let sharedViewModel:OAuthViewModel  =  OAuthViewModel()
    
    
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    
    //创建用户模型
    var userAccount:accessTokenModel?
    
    
    override init() {
        super.init()
        
        userAccount = getUserAccount()
    }
    
    
    var accessToken:String?{
        // 代表用户有accessToken
        if userAccount?.access_token != nil {
            // 看是否过期
            // 当前保存的日期与当前日期对比 如果为降序 为未过去
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
           
            }else {
                // 代表已经过期
                return nil
            }
        }else {
            // 代表没有acceToken
            /*
            - 代表用户没有登录 或者没有保存数据
            */
            return nil
        }
    }


    //判断用户是否登录,accessToken不等于空时说明已经登录
    var isLogin:Bool{
        
    return accessToken != nil
        
       
    }
    
    //保存用户数据
    func saveUserAccount(rootObject:accessTokenModel){
        
        //
        self.userAccount = rootObject
        print(rootObject.access_token)
        
         NSKeyedArchiver.archiveRootObject(rootObject, toFile: file)
        
        
    }
    
    //取出用户数据
    func getUserAccount() ->accessTokenModel?{
        
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? accessTokenModel
        
         return result
        
    
    }
    
   
    //获取asscee_token
    func loadUesrAccount(code:String,isSuccess:(isSucc:Bool)->()){
        
        NetworkTools.sharedManagerTolls.getAccessToken(code, success: { (response) -> () in
            
            //判断数据是否为空
            guard let res = response else{
                
                isSuccess(isSucc:false)
                return
            }
            
            //字典转模型
            let model =  accessTokenModel(dict: res as! [String : AnyObject])
            
            //获取成功以后继续获取用户信息
            self.loadeUserInfo(model, isSuccess: isSuccess)
            
            
            }) { (error) -> () in
                //失败
                isSuccess(isSucc: false)
        }
        
    }
    
    //获取用户信息
    func loadeUserInfo(model:accessTokenModel,isSuccess:(isSucc:Bool)->()){
        
        NetworkTools.sharedManagerTolls.getUserInfo(model, success: { (response) -> () in
            
            //成功
            guard let res = response else{
                isSuccess(isSucc: false)
                return
                
            }
            //字典转模型
            model.screen_name = res["screen_name"] as? String
            model.avatar_large = res["avatar_large"] as?String
            
            //保存数据到沙盒
            self .saveUserAccount(model)
            
            isSuccess(isSucc: true)
            
            }) { (error) -> () in
             
                isSuccess(isSucc: false)
        }
        
    }
    
    
}

 






